package com.wudizaba.jasper.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wudizaba.jasper.Util.JasperUtil;
import com.wudizaba.jasper.mapper.DishMapper;
import com.wudizaba.jasper.po.AResult;
import com.wudizaba.jasper.po.Dish;
import com.wudizaba.jasper.service.IDishService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author wudizaba
 * @since 2024-05-18
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    private String fileNameJasper;
    private Map<String, Object> params = new HashMap<>();

    @Resource
    @Qualifier(value = "jasperUtil")
    private JasperUtil expUtil;

    @Override
    public AResult getlistPdf(String fileName, Integer en) {

        if (en == 1) {
            fileName = fileName + ".pdf";
        }else if (en == 2) {
            fileName = fileName + ".html";
        }else if (en == 3) {
            fileName = fileName + ".xml";
        }else if (en == 4) {
            fileName = fileName + ".docx";
        } else if (en == 5) {
            fileName = fileName + ".xlsx";
        }else if (en == 6) {
            fileName = fileName + ".png";
        }else {
            return new AResult("导出格式错误!请检查导出格式是否正确!");
        }


        fileNameJasper = "jasper/jasperTest.jrxml";

        List<Dish> list = this.list();
        for (int i = 1; i <= 6; i++) {
            params.put("name" + i, list.get(i).getName());
            params.put("price" + i, list.get(i).getPrice().doubleValue());
            params.put("time" + i, new Date());
        }
        expUtil.exportPdfFromJasper(fileNameJasper, params, fileName,en);
        params.clear();
        System.out.println(" 导出成功。。。。。 ");
        return new AResult("导出成功");
    }


}
